﻿@using DevExpress.Blazor
@using EasyXaf.LowCode.RulesEngineEditors.Models
@using Microsoft.AspNetCore.Components.Web

<HeadContent>
    <style>
        .rule-context-menu-item img {
            width: 16px;
            height: 16px;
        }
    </style>
</HeadContent>

<div class="@NodeClassName">
    <div class="main-node">
        <div class="node-content-container">
            <div class="node-content" style="@NodeContentStyle" @ondblclick="PopupPropertyWindow">
                <div class="node-description">
                    @Rule.RuleDescription
                </div>
            </div>
            <RuleNodeAction class="action menu-action"
                            Title="菜单"
                            IconName="Action_More"
                            IconColor="#7F8C8D"
                            Click="ShowRuleContextMenuAsync" />
            @if (!IsStartNode)
            {
                if (Rule.ValidationResults.Any())
                {
                    <RuleNodeAction class="action validation-result-action"
                                    Title="@ValidationResultInfo"
                                    IconName="Actions_Info"
                                    IconColor="@ValidationResultIconColor"
                                    Click="ShowValidationResults" />
                }
                if (Rule.IsFirstRule)
                {
                    <RuleNodeAction class="action add-rule-top"
                                    Title="添加规则"
                                    IconName="Actions_AddCircled"
                                    Click="()=>AddRule(true)" />
                }
                if (!Rule.Rules.Any())
                {
                    <RuleNodeAction class="action add-rule-right"
                                    Title="添加子规则"
                                    IconName="Actions_AddCircled"
                                    Click="AddChildRuleAsync" />
                }
            }
            @if (Rule.IsLastRule)
            {
                <RuleNodeAction class="action add-rule-bottom"
                                Title="添加规则"
                                IconName="Actions_AddCircled"
                                Click="()=>AddRule(false)" />
            }
            @if (Rule.Rules.Any())
            {
                <RuleNodeAction class="action collapse-action"
                                Title="@(Rule.IsCollapse ? "展开" : "折叠")"
                                IconName="@(Rule.IsCollapse ? "ExpandSubordinates" : "CollapseSubordinates")"
                                Click="CollapseChildRulesAsync" />
            }
        </div>
        @if (!Rule.IsLastRule)
        {
            <div class="node-line">
                <div class="v-line"></div>
                @if (!IsReadOnly)
                {
                    <RuleNodeAction class="action insert-rule-action"
                                    Title="插入规则"
                                    IconName="Actions_AddCircled"
                                    Click="()=>AddRule(false)" />
                }
            </div>
        }
    </div>
    @if (!Rule.IsCollapse && ChildRules.Any())
    {
        <div class="child-nodes">
            <div class="node-line">
                <div class="h-line"></div>
                @if (Rule.Rules.Count > 1)
                {
                    <div class="action operator" title="规则操作符">
                        <span class="action-text operator-text @(Rule.Operator == RuleOperator.And ? "and" : "or")" @onclick="ShowOperatorContextMenuAsync">
                            @(Rule.Operator == RuleOperator.And ? "并" : "或")
                        </span>
                    </div>
                    @if (Rule.Operator == RuleOperator.Or)
                    {
                        <div class="action output-mode" title="嵌套规则输出方式">
                            <span class="action-text output-mode-text @(Rule.NestedRuleOutputMode == NestedRuleOutputMode.All ? "all" : "one")" @onclick="ShowNestedRuleOutputModeContextMenuAsync">
                                @(Rule.NestedRuleOutputMode == NestedRuleOutputMode.All ? "全部" : "一个")
                            </span>
                        </div>
                    }
                }
            </div>
            <div class="node-container">
                <RuleNodeContainer Rules="ChildRules" IsReadOnly="IsReadOnly" ContainerChanged="RuleChanged" />
            </div>
        </div>
    }
    <DxContextMenu @ref="RuleContextMenu">
        <Items>
            <DxContextMenuItem Text="置顶" CssClass="rule-context-menu-item" Enabled="CanMoveTop" Visible="!IsStartNode" IconUrl="IImageService/Actions_Arrow2Up" Click="()=>MoveRule(0)" />
            <DxContextMenuItem Text="上移" CssClass="rule-context-menu-item" Enabled="CanMoveUp" Visible="!IsStartNode" IconUrl="IImageService/Actions_Arrow4Up" Click="()=>MoveRule(RuleIndex-1)" />
            <DxContextMenuItem Text="下移" CssClass="rule-context-menu-item" Enabled="CanMoveDown" Visible="!IsStartNode" IconUrl="IImageService/Actions_Arrow4Down" Click="()=>MoveRule(RuleIndex+1)" />
            <DxContextMenuItem Text="置底" CssClass="rule-context-menu-item" Enabled="CanMoveBottom" Visible="!IsStartNode" IconUrl="IImageService/Actions_Arrow2Down" Click="()=>MoveRule(Rules.Count-1)" />
            <DxContextMenuItem Text="验证" CssClass="rule-context-menu-item" Enabled="!IsReadOnly" IconUrl="IImageService/BO_Validation" BeginGroup="true" Click="Validate" />
            <DxContextMenuItem Text="删除" CssClass="rule-context-menu-item" Enabled="!IsReadOnly" Visible="!IsStartNode" IconUrl="IImageService/Actions_Delete" BeginGroup="true" Click="DeleteRule" />
            <DxContextMenuItem Text="复制" CssClass="rule-context-menu-item" Enabled="!IsReadOnly" Visible="!IsStartNode" IconUrl="IImageService/Actions_Copy" BeginGroup="true" Click="CopyRule" />
            <DxContextMenuItem Text="剪切" CssClass="rule-context-menu-item" Enabled="!IsReadOnly" Visible="!IsStartNode" IconUrl="IImageService/Actions_Cut" Click="CutRule" />
            <DxContextMenuItem Text="粘贴" CssClass="rule-context-menu-item" Enabled="CanPaste" IconUrl="IImageService/Action_Paste" Click="PasteRule" />
            <DxContextMenuItem Text="属性" CssClass="rule-context-menu-item" IconUrl="IImageService/Properties" BeginGroup="true" Click="PopupPropertyWindow" />
        </Items>
    </DxContextMenu>
    <DxContextMenu @ref="OperatorContextMenu">
        <Items>
            <DxContextMenuItem Text="并" CssClass="rule-context-menu-item" Click="()=>ChangeRuleOperator(RuleOperator.And)" />
            <DxContextMenuItem Text="或" CssClass="rule-context-menu-item" Click="()=>ChangeRuleOperator(RuleOperator.Or)" />
        </Items>
    </DxContextMenu>
    <DxContextMenu @ref="NestedRuleOutputModeContextMenu">
        <Items>
            <DxContextMenuItem Text="全部" CssClass="rule-context-menu-item" Click="()=>ChangeNestedRuleOutputMode(NestedRuleOutputMode.All)" />
            <DxContextMenuItem Text="一个" CssClass="rule-context-menu-item" Click="()=>ChangeNestedRuleOutputMode(NestedRuleOutputMode.One)" />
        </Items>
    </DxContextMenu>
</div>
